//
//  NewsExtensions.m
//  Doc Bao
//
//  Created by Hai Nguyen on 9/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NewsExtensions.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation News (Extensions)

+(BOOL) initNews{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil)
	{
		return NO;
	}
	NSMutableArray *newsArray = [[[NSMutableArray alloc] init] autorelease];
	NSMutableDictionary *newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"1" forKey:@"Id"];
	[newsDict setObject:@"VNExpress" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"2" forKey:@"Id"];
	[newsDict setObject:@"Tuoi Tre Online" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"3" forKey:@"Id"];
	[newsDict setObject:@"Viet Nam Net" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"4" forKey:@"Id"];
	[newsDict setObject:@"Dan Tri" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"5" forKey:@"Id"];
	[newsDict setObject:@"Bao Dat Viet" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"6" forKey:@"Id"];
	[newsDict setObject:@"Thanh Nien Online" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	newsDict = [[NSMutableDictionary alloc] init];
	[newsDict setObject:@"7" forKey:@"Id"];
	[newsDict setObject:@"Nguoi Lao Dong" forKey:@"Name"];
	[newsDict setObject:@"vnexpress_logo.png" forKey:@"Logo"];
	[newsDict setObject:@"http://vnexpress.net" forKey:@"WebSite"];
	[newsArray addObject:newsDict];
	[newsDict release];
	
	for (int i = 0; i < [newsArray count]; i++) {
		NSDictionary *dict = [newsArray objectAtIndex:i];
		News *news = [NSEntityDescription insertNewObjectForEntityForName:@"News" inManagedObjectContext:managedObjectContext];
		news.Id = [NSNumber numberWithInt:[[dict objectForKey:@"Id"] intValue]];
		news.Name = [dict objectForKey:@"Name"];
		news.Logo = [dict objectForKey:@"Logo"];
		news.WebSite = [dict objectForKey:@"WebSite"];
	}
	[managedObjectContext save:&err];    
	
	return YES;
}

+(NSArray*) getNews{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"News" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+(News*) getNews:(int) newsId{
	News *news = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *newsArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"NewsId = %d", newsId];	
	@try {
		newsArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"News" :predicate :nil :NO :managedObjectContext];
		
		if ([newsArray count] != 0)
		{
			news = [newsArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return news;
}

@end
